﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class UpdateGroupLink : Portal.Interface.WebPart
    {
        private int MaxItems = 50;
    
        public override string WebPartId
        {
            get
            {
                return "UpdateGroupLink";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Cập nhật liên kết");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }



        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UpdateGroupLink));
            string GroupLinkId = System.Web.HttpContext.Current.Request["GroupLinkId"];
            GroupLinkCls GroupLinkCls = new GroupLinkBLL().CreateModel(GroupLinkId);
            if (GroupLinkCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }

            string SelectLanguageText =
           "    <select id=divSelectLanguage onchange=javascript:DetectSubjectLanguage('" + GroupLinkId + "');>";
            string[] Languages = Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "GroupLink.js") +
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateGroupLinkLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "UpdateGroupLinkCaption", "Tên nhóm") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtGroupLink value=\"" + GroupLinkCls.Subject + "\"></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveUpdateGroupLink('" + GroupLinkId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateGroupLinkSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "UpdateGroupLinkCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>" +
                              "<div id=divContentItems class=cssGroupLinkItems>" + GetItems(Environments.ActiveLanguage, GroupLinkId) + "</div>";

            RetHtml = Portal.Environment.Environments.GetFormDialog(RetHtml, ResourceBLL.GetText(WebPartId, "UpdateGroupLinkFormTitle", "Cập nhật liên kết"));
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetItems(string Language,string GroupLinkId)
        {
            try
            {
                string
                    RetHtml = "     <table>" +
                                    "  <tr>" +
                                    "      <th class=cssItemName>" + ResourceBLL.GetText(WebPartId, "LinkItemName", "Chi tiết") + "</th>" +
                                    "      <th class=cssItemUrl>" + ResourceBLL.GetText(WebPartId, "LinkItemUrl", "Url") + "</th>" +
                                    "      <th class=cssItemTarget>" + ResourceBLL.GetText(WebPartId, "LinkItemTarget", "Mục tiêu") + "</th>" +
                                    "      <th class=cssItemDescription>" + ResourceBLL.GetText(WebPartId, "LinkItemDescription", "Note") + "</th>" +
                                    "      <th class=cssItemIndexOrder>" + ResourceBLL.GetText(WebPartId, "LinkItemIndexOrder", "Thứ tự") + "</th>" +
                                    " </tr>";
                DataTable dtGroupLinkItems = new GroupLinkBLL().ReadingGroupLinkItems(GroupLinkId);
                int TotalRow = 0;
                for (int iIndex = 0; iIndex < dtGroupLinkItems.Rows.Count; iIndex++)
                {
                    GroupLinkItemCls GroupLinkItemCls = new GroupLinkItemCls();
                    string GroupLinkItemId = (string)dtGroupLinkItems.Rows[iIndex]["GroupLinkItemId"];
                    GroupLinkItemCls.GroupLinkItemId = GroupLinkItemId;
                    GroupLinkItemCls.GroupLinkItemName = "";
                    try
                    {
                        GroupLinkItemCls.GroupLinkItemName = (string)dtGroupLinkItems.Rows[iIndex][Language + "GroupLinkItemName"];
                    }
                    catch { }
                    GroupLinkItemCls.Url = (string)dtGroupLinkItems.Rows[iIndex]["Url"];
                    GroupLinkItemCls.Target = (string)dtGroupLinkItems.Rows[iIndex]["Target"];
                    GroupLinkItemCls.IndexOrder = (int)dtGroupLinkItems.Rows[iIndex]["IndexOrder"];
                    try
                    {
                        GroupLinkItemCls.Description = (string)dtGroupLinkItems.Rows[iIndex][Language+"Description"];
                    }
                    catch { }
                    TotalRow++;
                    RetHtml += "<tr>" +
                               "    <td><input type=hidden id=\"txtGroupLinkItemId_"+iIndex+"\" value=\""+GroupLinkItemId+"\"><input class=cssInputItemName value=\"" + GroupLinkItemCls.GroupLinkItemName + "\" id=\"txtItemName_" + iIndex + "\"></td>\r\n" +
                               "    <td><input class=cssInputItemUrl value=\"" + GroupLinkItemCls.Url + "\" id=\"txtItemUrl_" + iIndex + "\"></td>\r\n" +
                               "    <td><input value=\"" + GroupLinkItemCls.Target + "\" class=cssInputItemTarget id=\"txtItemTarget_" + iIndex + "\"></td>\r\n" +
                               "    <td><input value=\"" + GroupLinkItemCls.Description + "\" class=cssInputItemDescription id=\"txtItemDescription_" + iIndex + "\"></td>\r\n" +
                               "    <td><input value=\"" + GroupLinkItemCls.IndexOrder.ToString() + "\" class=cssInputItemIndexOrder value=\"" + (iIndex + 1) + "\" id=\"txtItemIndexOrder_" + iIndex + "\"></td>\r\n" +
                               "</tr>";
                }

                for (int zIndex = TotalRow; zIndex < MaxItems; zIndex++)
                {
                    RetHtml += "<tr>" +
                               "    <td><input type=hidden id=\"txtGroupLinkItemId_" + zIndex + "\" value=\"\"><input class=cssInputItemName  id=\"txtItemName_" + zIndex + "\"></td>\r\n" +
                               "    <td><input class=cssInputItemUrl  id=\"txtItemUrl_" + zIndex + "\"></td>\r\n" +
                               "    <td><input value=\"_blank\"  class=cssInputItemTarget id=\"txtItemTarget_" + zIndex + "\"></td>\r\n" +
                               "    <td><input class=cssInputItemDescription id=\"txtItemDescription_" + zIndex + "\"></td>\r\n" +
                               "    <td><input  class=cssInputItemIndexOrder value=\"" + (zIndex + 1) + "\" id=\"txtItemIndexOrder_" + zIndex + "\"></td>\r\n" +
                               "</tr>";
                }

                RetHtml +=
                     "</table>";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public GroupLinkItemCls[] GetGroupLinkItemsObject()
        {
            GroupLinkItemCls[] GroupLinkItems = new GroupLinkItemCls[MaxItems];
            for (int iIndex = 0; iIndex < GroupLinkItems.Length; iIndex++)
            {
                GroupLinkItems[iIndex] = new GroupLinkItemCls();
            }
            return GroupLinkItems;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateGroupLink(
            string Language,
            string GroupLinkId, 
            string GroupLinkName, 
            GroupLinkItemCls[] GroupLinkItems)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = new GroupLink().FindPermissionType("GroupLinkItems");
                GroupLinkCls GroupLinkCls = new GroupLinkBLL().CreateModel(GroupLinkId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateGroupLinkItem", GroupLinkId,GroupLinkCls.Owner, true);

                if (GroupLinkName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên"));
                new GroupLinkBLL().UpdateGroupLink(null,Language, GroupLinkId, GroupLinkName,GroupLinkItems);
                return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}